var fs = require("fs");
var _ = require("lodash");

let fileNameB = "../extra";
let fileNameC = "./data/core";
let outputFileName = "./lua/core.lua";

let extraData = JSON.parse(fs.readFileSync(`${fileNameB}.json`).toString()).core;
let mainData = JSON.parse(fs.readFileSync(`${fileNameC}.json`).toString());

let extraMap = new Map();
_.forEach(extraData, (e) => {
    let id = e.id;
    extraMap.set(id, e);
});

let output = `local p = {}
`

_.forEach(mainData, (e) => {
    let data = e.data;
    let exData = extraMap.has(e.id) ? extraMap.get(e.id) : {};
    let skill1, skill2;
    if (data.coreStage) {
        let skill1_1 = data.coreStage[2].skill1;
        let skill1_2 = data.coreStage[3].skill1;
        skill1 = {
            center: skill1_1.centerIcon || 0,
            element: _.get(skill1_1,"paSkillFuncition[0].element") || skill1_1.centerIcon || 0, 
            param: exData.skill1_param || skill1_1.paramIcon,
            series: skill1_1.seriesIcon,
            level: [skill1_1.levelIcon],
            name: [skill1_1.name],
        }
        if (skill1_2.levelIcon > skill1_1.levelIcon) {
            skill1.level.push(skill1_2.levelIcon);
            skill1.name.push(skill1_2.name);
        }
        let skill2_1 = data.coreStage[2].skill2;
        let skill2_2 = data.coreStage[3].skill2;
        if (skill2_1 || skill2_2) {
            skill2 = {
                center: skill2_2.centerIcon || 0,
                element: _.get(skill2_2,"paSkillFuncition[0].element") || skill2_2.centerIcon || 0,
                param: exData.skill2_param || skill2_2.paramIcon,
                series: skill2_2.seriesIcon,
                level: [0, skill2_2.levelIcon],
                name: [skill2_2.name],
            }
            if (skill2_1 && skill2_1.level < skill2_2.level) {
                skill2.level = [skill2_1.levelIcon, skill2_2.levelIcon];
                skill2.name = [skill2_1.name, skill2_2.name];
            }
        }
    }
    
    let str = `p['${e.id}'] = {
    ['name'] = '${data.name}',
    ['element'] = '${data.coreElement}',
    ['minAtk'] = '${data.coreMinAtk}',
    ['minHp'] = '${data.coreMinHp}',
    ['maxAtk'] = '${data.coreMinAtk + 59 * data.coreAddAtk}',
    ['maxHp'] = '${data.coreMinHp + 59 * data.coreAddHp}',
    ['rarity'] = '${data.coreRarity}',
    ['series'] = '${data.coreSeries}',
    ['prefix'] = '${data.prefix}',
    ['cnData'] = '${data.cnData}',
    ['addDate'] = ${exData.add_date || "''"},
    `
    if (skill1) {
        str += 
`['skill1'] = {
        ['center'] = ${skill1.center},
        ['element'] = ${skill1.element},
        ['param'] = ${skill1.param},
        ['series'] = ${skill1.series},
        ['level'] = ${JSON.stringify(skill1.level).replace('[','{').replace(']','}')},
        ['name'] = ${JSON.stringify(skill1.name).replace('[','{').replace(']','}')},
    },
    `
    }
    if (skill2) {
        str += 
`['skill2'] = {
        ['center'] = ${skill2.center},
        ['element'] = ${skill2.element},
        ['param'] = ${skill2.param},
        ['series'] = ${skill2.series},
        ['level'] = ${JSON.stringify(skill2.level).replace('[','{').replace(']','}')},
        ['name'] = ${JSON.stringify(skill2.name).replace('[','{').replace(']','}')},
    },
    `
    }
    str += `
}
`;
    output += str;
});
output += `return p`

fs.writeFileSync(outputFileName, output);